/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.gpsgame.client.cv.activity;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import es.sonxurxo.gpsgame.client.R;
import es.sonxurxo.gpsgame.client.cv.common.dialogs.CommonDialogs;
import es.sonxurxo.gpsgame.client.model.service.player.HttpPlayerServiceImpl;
import es.sonxurxo.gpsgame.client.model.service.to.PublicPlayerProfileTO;
import es.sonxurxo.gpsgame.client.util.exception.ServerException;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
public class ViewPublicPlayerProfileActivity extends Activity {
	
	private TextView loginView, totalPointsView, maxPointsView, bestRankView,
    countryView, emailView, phoneView, nameView, surnameView;
	
	private ImageView image;
	
	private PublicPlayerProfileTO pPPTO;
	
	private String login;
	
	private ViewPublicPlayerProfileTask vPPPTask;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.view_public_player_profile_page);
        login = getIntent().getExtras().getString("login");
        
    	loginView = (TextView)findViewById(R.id.pi_tv_login);
        emailView = (TextView)findViewById(R.id.pi_tv_email);
        phoneView = (TextView)findViewById(R.id.pi_tv_phone);
        nameView = (TextView)findViewById(R.id.pi_et_name);
        surnameView = (TextView)findViewById(R.id.pi_et_surname);
        countryView = (TextView)findViewById(R.id.pi_et_country);
        totalPointsView = (TextView)findViewById(R.id.pi_tv_totalpoints);
        maxPointsView = (TextView)findViewById(R.id.pi_tv_maxpoints);
        bestRankView = (TextView)findViewById(R.id.pi_tv_bestrank);
        image = (ImageView)findViewById(R.id.pi_image);
        
        launchViewPublicPlayerProfileThread();
	}
	
	private void doViewPublicPlayerProfile() {
		image.setImageResource(pPPTO.getImageId());

        loginView.setText(pPPTO.getLogin());
        if (pPPTO.isShowPersonalInfo()) {
        	emailView.setText(pPPTO.getEmail());
	        phoneView.setText(pPPTO.getPhone());
	        nameView.setText(pPPTO.getName());
	        surnameView.setText(pPPTO.getSurname());
        }
        else {
        	emailView.setText("Not shown");
	        phoneView.setText("Not shown");
	        nameView.setText("Not shown");
	        surnameView.setText("Not shown");
        }
        countryView.setText(pPPTO.getCountry());        
        totalPointsView.setText(String.valueOf(pPPTO.getTotalPoints()));
        maxPointsView.setText(String.valueOf(pPPTO.getMaxPoints()));
        bestRankView.setText(String.valueOf(pPPTO.getBestRank()));

	}

	private void launchViewPublicPlayerProfileThread() {
		vPPPTask = new ViewPublicPlayerProfileTask(login);
		Thread vPPPThread = new Thread(null, vPPPTask, "Login");
		vPPPThread.start();
		showDialog(CommonDialogs.CONNECTING_TO_SERVER_DIALOG_ID);
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
    	return CommonDialogs.createDialog(id, this);
    }
	
	private class ViewPublicPlayerProfileHandler extends Handler {

		public ViewPublicPlayerProfileHandler(Looper looper) {
			super(looper);
		}
		
		@Override
		public void handleMessage(Message msg) {
			dismissDialog(CommonDialogs.CONNECTING_TO_SERVER_DIALOG_ID);
			ServerException sE = 
				(ServerException)msg.getData().getSerializable("ServerException");
			if (sE	!= null) {
				CommonDialogs.errorMessage = sE.getMessage();
	        	showDialog(CommonDialogs.SERVER_ERROR_DIALOG_ID);
	        	return;
	        }
        	Exception e = 
	        	(Exception)msg.getData().getSerializable("Exception");
	        	if (e != null) {
	        		CommonDialogs.errorMessage = e.getLocalizedMessage();
		        	showDialog(CommonDialogs.CLIENT_ERROR_DIALOG_ID);
		        	return;
	        	}
			PublicPlayerProfileTO pPPTO2 = 
				(PublicPlayerProfileTO)msg.getData().getSerializable("pPPTO");
			if (pPPTO2 != null) {
				pPPTO = pPPTO2;
				doViewPublicPlayerProfile();
			}
		}
	}
	
	private class ViewPublicPlayerProfileTask implements Runnable {

		String login;
		HttpPlayerServiceImpl playerService;
		
		ViewPublicPlayerProfileTask(String login) {
			this.login = login;
			playerService = new HttpPlayerServiceImpl();
		}
		
		public void run() {
			
			ViewPublicPlayerProfileHandler handler = new ViewPublicPlayerProfileHandler(Looper.getMainLooper());
			Bundle data = new Bundle();
			Message msg = new Message();
			try {
				PublicPlayerProfileTO pPPTO = 
					playerService.getPublicPlayerProfile(login);
				data.putSerializable("pPPTO", pPPTO);
				msg.setData(data);
				handler.sendMessage(msg);
				
			} catch (ServerException e) {
	        	data.putSerializable("ServerException", e);
	        	msg.setData(data);
				handler.sendMessage(msg);
	        } catch (Exception e) {
	        	data.putSerializable("Exception", e);
	        	msg.setData(data);
				handler.sendMessage(msg);
	        }
		}
	}
}
